<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="/WEB-INF/taglib/c.tld" %>
<%@ taglib uri="/WEB-INF/taglib/fn.tld" prefix="fn" %>
<%    
String path = request.getContextPath();    
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";    
pageContext.setAttribute("basePath",basePath);    
%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8"/>
<title>i HelloWorld</title>
<base href="<%=basePath %>" />
<link rel="stylesheet" type="text/css" href="js/easyui/themes/default/easyui.css"/>
<link rel="stylesheet" type="text/css" href="js/easyui/themes/icon.css"/>
<script type="text/javascript" src="js/jquery-2.0.3.js"></script>
<script type="text/javascript" src="js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="js/easyui/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("#dg").datagrid({
		url:"admin/catagory/query.do",
		fitColumns:true,
		toolbar: "#tb",
		method:"post",
		idField:"id",
		singleSelect:true,
		checkOnSelect:false,
		selectOnCheck:false,
		onClickRow:edit,
		columns:[[
			 {field:"id",title:"标识码",checkbox:true,align:"center",width:100},
			 {field:"code",title:"简码",align:"center",width:100,editor:{type:'text'}},
			 {field:"name",title:"名称",align:"center",editor:"text",width:100,editor:{type:"text"}},
			 {field:"turn",title:"顺序",align:"center",editor:"numberbox",width:50,editor:"numberbox"},
			 {field:"intro",title:"简介",align:"center",editor:"text",width:200,
			 	editor:{
					type:'text'
				}
			}
		]]
	});
});
var editIndex=undefined;
function add(){
	if(editIndex!=undefined){
		if ($('#dg').datagrid('validateRow', editIndex)){
			$("#dg").datagrid('endEdit',editIndex);
		}else{
			$("#dg").datagrid('cancelEdit',editIndex);
		}
	}
	$("#dg").datagrid('appendRow', {});
	var rows = $("#dg").datagrid('getRows');
	$("#dg").datagrid('beginEdit', rows.length - 1);
	editIndex=rows.length-1;
}
function edit(){
	if(editIndex!=undefined){
		if ($('#dg').datagrid('validateRow', editIndex)){
			$("#dg").datagrid('endEdit',editIndex);
		}else{
			$("#dg").datagrid('cancelEdit',editIndex);
		}
	}
	var row = $("#dg").datagrid('getSelected');
	if (row) {
		var rowIndex = $("#dg").datagrid('getRowIndex', row);
		$("#dg").datagrid('beginEdit', rowIndex);
		editIndex=rowIndex;
	} 
}
function rmov(){
	var rows = $("#dg").datagrid('getChecked');
	var id="";
	for(var i=0;i<rows.length;i++){
		if(i!=rows.length-1){
			id=id+rows[i].id+",";
		}else{
			id=id+rows[i].id;
		}
	}
	$.post("admin/catagory/delete.do",{ids:id},function(rs){
		if(rs.status){
			$('#dg').datagrid('reload');
		}
	});
	//var rowIndex = $("#dg").datagrid('getRowIndex',id);
	//$("#dg").datagrid('deleteRow', rowIndex);		
}
function accept(){
	if(editIndex!=undefined){
		if ($('#dg').datagrid('validateRow', editIndex)){
			$("#dg").datagrid('endEdit',editIndex);
		}else{
			$("#dg").datagrid('cancelEdit',editIndex);
		}
	}
	var rows = $('#dg').datagrid('getChanges');
	var list=JSON.stringify(rows); 
	var bean={};
	bean.list=list;
	
	$.post("admin/catagory/add.do",bean,function(rs){
		if(rs.status){
			$('#dg').datagrid('reload');
		}
	});
}
function reject(){
	$('#dg').datagrid('rejectChanges');
}
</script>
</head>

<body>
	<table id="dg"></table>
    <div id="tb">
    	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="add()">增加</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="rmov()">删除</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">保存</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
    </div>
</body>
</html>